<template>
  <div class="page-brand">
    <el-tabs
      v-model="tabValue"
      @tab-change="handleQuery"
    >
      <el-tab-pane
        :label="$t('brand.allBrand')"
        name="allBrand"
      >
        <div class="search-bar">
          <el-form
            ref="searchFormRef"
            :inline="true"
            class="search-form"
            :model="searchForm"
            @submit.prevent
          >
            <div class="input-row">
              <el-form-item
                prop="name"
                :label="$t('brand.brandName')+':'"
                class="search-form-item"
              >
                <el-input
                  v-model.trim="searchForm.name"
                  clearable
                  :placeholder="$t('brand.brandName')"
                />
              </el-form-item>
              <el-form-item
                prop="status"
                :label="$t('product.status')+':'"
                class="search-form-item"
              >
                <el-select
                  v-model="searchForm.status"
                  clearable
                  :placeholder="$t('product.status')"
                >
                  <el-option
                    :label="$t('brand.lowerShelf')"
                    :value="0"
                  />
                  <el-option
                    :label="$t('publics.normal')"
                    :value="1"
                  />
                </el-select>
              </el-form-item>
              <el-form-item>
                <div
                  class="default-btn primary-btn"
                  @click="searchChange(true)"
                >
                  {{ $t('crud.searchBtn') }}
                </div>
                <div
                  class="default-btn"
                  @click="resetSearchForm"
                >
                  {{ $t('product.reset') }}
                </div>
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div class="main-container">
          <div class="operation-bar">
            <div
              class="default-btn primary-btn"
              @click.stop="addOrUpdateHandle()"
            >
              {{ $t('crud.addTitle') }}
            </div>
          </div>
          <div class="table-con spec-table">
            <el-table
              ref="specListTable"
              :data="dataList"
              header-cell-class-name="table-header"
              row-class-name="table-row"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="100"
                :label="$t('brand.serialNumber')"
              />

              <el-table-column
                prop="name"
                :label="$t('brand.brandName')"
                width="300"
              >
                <template #default="scope">
                  <div class="table-cell-text">
                    {{ scope.row.name }}
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                prop="imgUrl"
                :label="$t('brandPopups.brandLogo')"
              >
                <template #default="scope">
                  <div class="table-cell-image">
                    <ImgShow :src="scope.row.imgUrl" />
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                prop="firstLetter"
                :label="$t('brandPopups.brandInitials')"
                width="120"
              />

              <el-table-column
                prop="seq"
                :label="$t('brandPopups.seq')"
                width="80"
              />

              <el-table-column
                prop="status"
                :label="$t('product.status')"
                width="100"
              >
                <template #default="scope">
                  <span
                    v-if="scope.row.status === 0"
                    class="tag-text"
                  >{{ $t("brand.lowerShelf") }}</span>
                  <span
                    v-if="scope.row.status === 1"
                    class="tag-text"
                  >{{ $t("brand.normal") }}</span>
                  <span
                    v-if="scope.row.status === 2"
                    class="tag-text"
                  >待审核</span>
                </template>
              </el-table-column>

              <!-- <el-table-column
                prop="createBy"
                :label="$t('brand.createBy')"
                width="150"
              >
                <template #default="scope">
                  <div class="table-cell-text">
                    {{ scope.row.createBy || '--' }}
                  </div>
                </template>
              </el-table-column> -->

              <el-table-column
                prop="createTime"
                :label="$t('brand.createTime')"
                width="200"
              >
                <template #default="scope">
                  <div class="table-cell-text">
                    {{ scope.row.createTime || '--' }}
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                align="center"
                fixed="right"
                :label="$t('publics.operating')"
                width="180"
              >
                <template #default="scope">
                  <div class="text-btn-con">
                    <div
                      class="default-btn text-btn"
                      @click="addOrUpdateHandle(scope.row.brandId)"
                    >
                      {{ $t('crud.updateBtn') }}
                    </div>
                    <div
                      v-if="scope.row.status === 0"
                      class="default-btn text-btn"
                      @click="enableOrDisable(scope.row.brandId,scope.row.status)"
                    >
                      <!-- 上架 -->
                      {{ $t('publics.UpperShelf') }}
                    </div>
                    <div
                      v-if="scope.row.status === 1"
                      class="default-btn text-btn"
                      @click="enableOrDisable(scope.row.brandId,scope.row.status)"
                    >
                      <!-- 下架 -->
                      {{ $t('publics.LowerShelf') }}
                    </div>
                    <div
                      class="default-btn text-btn"
                      @click="deleteHandle(scope.row.brandId)"
                    >
                      {{ $t('brand.delete') }}
                    </div>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <el-pagination
            v-if="dataList.length"
            :current-page="page.currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-tab-pane>
      <el-tab-pane
        :label="$t('brand.auditBrand')"
        name="approveBrand"
      >
        <approveBrand v-if="tabValue=='approveBrand'" />
      </el-tab-pane>
    </el-tabs>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import addOrUpdate from './components/add-or-update.vue'
import approveBrand from './components/approve-brand.vue'

import { ElMessage, ElMessageBox } from 'element-plus'
import { onMounted, reactive } from 'vue'

const tabValue = ref('allBrand')
const Data = reactive({
  dataForm: {
    indexImg: ''
  },
  dataList: [],
  dataListLoading: false,
  dataListSelections: [],
  addOrUpdateVisible: false,
  // 修改
  page: {
    total: 0, // 总页数
    currentPage: 1, // 当前页数
    pageSize: 10 // 每页显示多少条
  },
  searchForm: {
    status: ''
  }
})

const { dataList, addOrUpdateVisible, page, searchForm } = toRefs(Data)

onMounted(() => {
  getDataList()
})

// 获取数据列表
const getDataList = (page, newData = false) => {
  if (Data.page) {
    const size = Math.ceil(Data.page.total / Data.page.pageSize)
    Data.page.currentPage = (Data.page.currentPage > size ? size : Data.page.currentPage) || 1
  }
  if (newData || !Data.theData) {
    Data.theData = JSON.parse(JSON.stringify(Data.searchForm))
  }
  Data.dataListLoading = true
  http({
    url: http.adornUrl('/platform/brand/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: page == null ? Data.page.currentPage : page.currentPage,
          size: page == null ? Data.page.pageSize : page.pageSize
        },
        Data.theData
      )
    )
  }).then(({ data }) => {
    Data.dataList = data.records
    Data.page.total = data.total
    Data.dataListLoading = false

    // 末尾页数据为空重新请求
    if (!Data.dataList.length && data.current > 1) {
      Data.page.currentPage = 1
      getDataList()
    }
  })
}

const addOrUpdateRef = ref()
// 新增 / 修改
const addOrUpdateHandle = (id) => {
  Data.addOrUpdateVisible = true
  nextTick(() => {
    addOrUpdateRef.value.init(id)
  })
}

// 删除
const deleteHandle = (id) => {
  const ids = id ? [id] : Data.dataListSelections.map(item => {
    return item.brandId
  })
  ElMessageBox.confirm($t('remindPop.makeSure') + ' ' + `[${id ? $t('remindPop.delete') : $t('remindPop.batchDeletion')}]` + ' ' + $t('remindPop.operation') + '?', $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/platform/brand'),
      method: 'delete',
      params: {
        brandId: id
      }
    }).then(() => {
      Data.page.total = Data.page.total - ids.length
      ElMessage({
        message: $t('remindPop.succeeded'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  })
}

/**
* 上架/下架品牌
*/
const enableOrDisable = (brandId, sts) => {
  const brandObj = {}
  brandObj.brandId = brandId
  brandObj.status = sts === 0 ? 1 : 0
  ElMessageBox.confirm(sts === 1 ? $t('brandPopups.isConfirmOffline') : $t('brandPopups.isConfirmUpload'), $t('text.tips'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/platform/brand/updateBrandStatus'),
      method: 'put',
      data: brandObj
    }).then(() => {
      ElMessage({
        message: $t('remindPop.succeeded'),
        type: 'success',
        duration: 1500,
        onClose: () => getDataList(Data.page)
      })
    })
  }).catch(() => { })
}

// 条件查询
const searchChange = (newData = false) => {
  Data.page.currentPage = 1
  getDataList(Data.page, newData)
}

// 刷新回调
const refreshChange = () => {
  getDataList(Data.page)
}

const searchFormRef = ref()
/**
* 重置表单
* @param {String} formName 表单名称
*/
const resetSearchForm = () => {
  searchFormRef.value.resetFields()
}

const handleSizeChange = (val) => {
  Data.page.pageSize = val
  getDataList()
}

const handleCurrentChange = (val) => {
  Data.page.currentPage = val
  getDataList()
}

const handleQuery = (e) => {
  console.log(tabValue.value)
  if (tabValue.value == 'allBrand') {
    getDataList()
  }
}
</script>

<style lang="scss" scoped>

</style>
